package com.puppet.building182.service.impl;

import com.puppet.building182.mapper.RoleMapper;
import com.puppet.building182.pojo.Building;
import com.puppet.building182.pojo.Role;
import com.puppet.building182.pojo.VO.RoleVo;
import com.puppet.building182.service.RoleService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.rmi.server.UID;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    RoleMapper roleMapper;

    @Override
    public List<RoleVo> getRole( Integer uid) {
        List<RoleVo> allRoles = roleMapper.getAllRole();
        List<RoleVo> roleByUid=this.getRolesByUid(uid);
        //将所有角色信息中，哪个行应该被选中，哪个行不应该被选中【1，2，3，4】  【1，3】
   for (RoleVo allRole : allRoles) {
       for(RoleVo userRole :roleByUid){
           if(userRole.getRoleId()== allRole.getId()){
               allRole.setFlg(true);
           }
       }
   }
        System.out.println(allRoles);
   return allRoles;
    }

    @Override
    public List<RoleVo> getRolesByUid(Integer id) {

        return roleMapper.getRolesByUid(id);
    }

    @Override
    @Transactional
    public Integer saveRoleByUid(Integer uid, List<Integer> roleIds) {
        Integer num=roleMapper.deleteRoleByUid(uid);
        if(!roleIds.isEmpty()){
            num=roleMapper.saveNewRoles(uid,roleIds);
        }
        return num;
    }

    @Override
    public Integer countRoleByUserId(Integer id) {
        return roleMapper.countRoleByUserId();

    }

    //正式流程

    @Override
    public List<RoleVo> getAllRoles() {
        return roleMapper.getAllRole();
    }

    @Override
    public Integer updateRole(RoleVo roleVo) {
        return roleMapper.updateRole(roleVo);
    }

    @Override
    public RoleVo getrolebyid(Integer id) {
        return roleMapper.getrolebyid(id);
    }

    @Override
    public Integer deleteRoleById(Integer id) {
        return roleMapper.deleteRoleById(id);

    }
}
